# -*- coding:utf-8 -*-
from TestBase.browser_action import Base
from TestBase.webdriver_package import *


class TicketPage(Base):

    def __init__(self, case_instance):
        super(TicketPage, self).__init__(case_instance, csv_file="ticket_page.csv")

    # 检查车次是否存在,点击进入购买页面
    def choice_station(self):
        # 选择车票类型(复选框): 根据"车票类型节点"元素，通过参数数据，定位到指定的车票类型元素
        ticket_type_node_ele = self.ticket_type_node()
        ticket_type_param = self.data('ticket_type')
        ticket_type_ele = self.find_ele(by=By.ID, value=f"type|{ticket_type_param}", parent_ele=ticket_type_node_ele)
        ticket_type_ele.click()
        time.sleep(2)

        # 选择出发时间(复选框)：同上
        depart_time_node_ele = self.depart_time_node()
        depart_time_param = self.data('depart_time')
        depart_time_ele = self.find_ele(by=By.ID, value=f"dtr|{depart_time_param}", parent_ele=depart_time_node_ele)
        depart_time_ele.click()
        time.sleep(2)

        # 根据参数数据获取指定的车次号
        train_no_list = self.train_no_list()  # 获取车次号列表
        train_no = train_no_list[int(self.data("train_num")) - 1].text

        # 验证车次号是否正确
        self.screenshot(img_no=1)
        self.case_instance.assertEqual(train_no, self.data("assert_trainNo"), "车次号验证失败")

        # 点击'二等座'购买
        buy_btn_list = self.buy_btn_list()  # 获取二等座购买按钮列表
        buy_btn = buy_btn_list[int(self.data("train_num")) - 1]
        buy_btn.click()
        time.sleep(2)

        # 验证 跳转页面是否正确
        self.screenshot(img_no=2)
        self.assert_content(content=self.data("assert_trainNo"), error_msg="车票购买页跳转失败")
        time.sleep(2)
